package com.mangguo.tingjian.content.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mangguo.tingjian.content.dto.ContentCountResult;
import com.mangguo.tingjian.content.dto.CountResult;
import com.mangguo.tingjian.content.entity.UserSubscription;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 用户订阅的专辑-同步 Mapper 接口
 * </p>
 *
 * @author auto
 * @since 2019-11-28
 */
@Mapper
public interface UserSubscriptionMapper extends BaseMapper<UserSubscription> {
    @Select("select count(1) from dw_user_subscription where mdate >= #{start} and mdate <= #{end} and cid=#{contentId} and type=#{contentType}")
    Long countSubscriptionBetweenDate(@Param("start")Integer start,
                                       @Param("end") Integer end,
                                       @Param("contentId") Long contentId,
                                       @Param("contentType") Integer contentType);

    @Select("select count(1) as count,mdate from dw_user_subscription where mdate >= #{start} and mdate <= #{end} and albumId=#{albumId}  group by mdate")
    List<CountResult> countSubscriptionByAlbumIdBetweenDate(@Param("start")Integer start,
                                                            @Param("end") Integer end,
                                                            @Param("albumId") Long albumId);

    @Select("<script>select count(1) as count,mdate,cid as contentId " +
            " from dw_user_subscription where mdate &gt;= #{start} and mdate &lt;= #{end} and " +
            "<if test='albumIds != null and albumIds.size()>0'> "+
            "cid IN " +
            "<foreach item='albumId' collection='albumIds' open='(' close=')' separator=','> "+
            "#{albumId}"+
            "</foreach> AND "+
            "</if>"+
            " type = #{type} " +
            "group by mdate,cid" +
            "</script>")
    List<ContentCountResult> countSubscriptionByAlbumIdBetweenDates(@Param("start")Integer start,
                                                                    @Param("end") Integer end,
                                                                    @Param("albumIds") List<Long> albumIds,
                                                                    @Param("type") Integer type);

}
